class CommentController < ApplicationController
  before_filter :protect
  before_filter :load_requirement, :except =>[:download,:index]
  
  def index
    @comment = Comment.find(params[:id])
    @title = @comment.user.username+"的稿件"
  end
  
  def create
    @comment = Comment.new(params[:comment])
    @comment.user = @login_user
    @comment.requirement = @requirement
    if @comment.duplicate? or (@comment.save && @requirement.comments << @comment)
      flash[:notice] = "评论成功"
    else
      flash[:notice] = "评论失败"
    end
    redirect_to :controller => :requirement, :action => :show, :id => @requirement
  end
  
  def destroy
    @comment = Comment.find(params[:id])
    if @comment.authorized?(@login_user)
      @comment.destroy
      flash[:notice] = "成功删除#{@login_user.username}的评论"
    else
      flash[:notice] = "删除评论失败"
    end
    redirect_to :controller => :requirement, :action => :show, :id => @requirement
  end
  
  def download
    comment = Comment.find(params[:id])
    if comment
      send_file comment.attach.path, :filename => comment.attach_file_name    #:type=>"application/zip"  
    end
     
  end
  
  private
  def load_requirement
    @requirement = Requirement.find(params[:requirement_id])
  end
end
